<?php


namespace app\api\model;


use ApiOper;
use app\common\library\himongo;
use app\common\model\BaseModel;
use common;
use hiredis;
use think\Db;

class WarehouseModel extends BaseModel
{
    protected $name = 'warehouse';
    protected $pk = 'id';

    function save_to_mongo($city_id, $id) {
        $where = [
            'eftype' => EB_TYPE_WAREHOUSE,
            'city_id' => $city_id,
            'id' => $id,
        ];

        himongo::db()->delete($where);

        $info = $this->query_info($city_id, $id);
        if ($info['state'] == 0 && $info['is_deleted'] == 0) {
            $data = common::type_conver($info, $this->mongo_field);
            $data['eftype'] = EB_TYPE_WAREHOUSE;
            $data['point'] = [
                'type' => 'Point',
                'coordinates' => $data['coordinate'],
            ];
            himongo::db()->insert($data);
        }
    }

    function query_info($city_id, $warehouse_id) {
        $selectlist = [
            'id' => 'string',
            'city_id' => 'string',
            'name' => 'string',
            'remark' => 'string',
            'coordinate' => 'array',
            'state' => 'int',
            'purpose_type' => 'string',
            'entity_type' => 'string',
            'purpose' => 'string',
            'capacity' => 'int',
            'quantity' => 'int',
            'address' => 'string',
            'creator_id' => 'string',
            'create_time' => 'string',
            'creator_name' => 'string',
            'updater_id' => 'string',
            'update_time' => 'string',
            'updater_name' => 'string',
            'is_deleted' => 'int',
        ];

        $sfs = common::array_keys($selectlist);

        $beans = [
            'city_id'=>$city_id,
            'id'=>$warehouse_id
        ];

        $rows = $this->where($beans)->field($sfs)->find();
        if (empty($rows)) {
            return null;
        }
        return $rows;
    }

    function update_warehouse($city_id, $id, $info) {
        $paramlist = [
            'name' => 'string',
            'remark' => 'string',
            'coordinate' => 'array',
            'state' => 'int',
            'purpose_type' => 'string',
            'entity_type' => 'string',
            'purpose' => 'string',
            'address' => 'string',
        ];

        $data = common::type_conver($info, $paramlist);
        $info['update_time'] = date('Y-m-d H:i:s');

        $where = [
            'city_id' => $city_id,
            'id' => $id,
        ];
        self::update($data, $where);

        $this->save_to_mongo($city_id, $id);
    }

    function delete_warehouse($city_id, $id) {
        $data = [
            'is_deleted' => 1,
            'update_time' => date('Y-m-d H:i:s'),
        ];

        $where = [
            'city_id' => $city_id,
            'id' => $id,
        ];

        self::update($data, $where);

        $this->save_to_mongo($city_id, $id);
    }

    function WAREHOUSE_ID($city_id) {
        $key = 'WAREHOUSE_ID|' . $city_id;

        $id = hiredis::db()->incr($key);
        if ($id == 1) {
            $max_id = Db::table('tb_warehouse')->where(['city_id'=>$city_id])->max('id');

            if ($max_id === NULL) {
                $id = $city_id . "0001";
            } else {
                $id = strval(intval($max_id) + 1);
            }
            hiredis::db()->set($key, $id);
        }
        return strval($id);
    }

    function add($info) {
        $paramlist = [
            'city_id' => 'string',
            'name' => 'string',
            'remark' => 'string',
            'coordinate' => 'array',
            'state' => 'int',
            'creator_id' => 'string',
            'creator_name' => 'string',
            'purpose_type' => 'string',
            'entity_type' => 'string',
            'purpose' => 'string',
            'quantity' => 'int',
            'address' => 'string',
        ];

        $info = common::type_conver($info, $paramlist);

        if (!$info) {
           $this->throw_myexception(X_ERROR, "service area info error!");
        }

        $info['id'] = $this->WAREHOUSE_ID($info['city_id']);
        $info['create_time'] = date('Y-m-d H:i:s');
        ApiOper::instance()->append_creater_info($info);

        self::create($info);


        $this->save_to_mongo($info['city_id'], $info['id']);

        return $info;
    }

    function query_list($city_id, $condition, $pos, $limit) {
        $selectlist = [
            'id' => 'string',
            'city_id' => 'string',
            'name' => 'string',
            'remark' => 'string',
            'coordinate' => 'array',
            'state' => 'int',
            'purpose_type' => 'string',
            'entity_type' => 'string',
            'purpose' => 'string',
            'quantity' => 'int',
            'address' => 'string',
            'creator_id' => 'string',
            'create_time' => 'string',
            'creator_name' => 'string',
            'updater_id' => 'string',
            'update_time' => 'string',
            'updater_name' => 'string',
        ];

        $sfs = common::array_keys($selectlist);

        $beans = [
            'city_id'=>$city_id,
            'is_deleted' => 0
        ];

        if (isset($condition['state']) && $condition['state'] != -1) {
            $beans['state'] = $condition['state'];
        }

        if (isset($condition['entity_type']) && !empty($condition['entity_type'])) {
            $beans['entity_type'] = $condition['entity_type'];
        }
        if (isset($condition['purpose_type']) && !empty($condition['purpose_type'])) {
            $beans['purpose_type'] = $condition['entity_type'];
        }

        if (isset($condition['start_time'])) {
            $beans['create_time'] = ['>=',$condition['start_time']];
        }

        if (isset($condition['end_time'])) {
            $beans['create_time'] = ['<=',$condition['end_time']];
        }

        if (!empty($condition['sk']) && !empty($condition['sk'])) {
            if (ctype_alnum($condition['sk'])) {
                $beans['id'] = $condition['sk'];
            } else if (common::is_name($condition['sk'])) {
                $beans['name'] = $condition['sk'];
            } else {
                return [];
            }
        }

        $rows = $this->where($beans)->field($sfs)->limit($pos,$limit)->select();
        $l = [];
        foreach ($rows as $row) {
            $l[] = common::sql_row($selectlist, $row);
        }
        return $l;
    }

    function query_count($city_id, $condition) {

        $beans = [
            'city_id' => $city_id,
        ];

        if (isset($condition['state']) && $condition['state'] != -1) {
            $beans['state'] = $condition['state'];
        }

        if (isset($condition['entity_type']) && !empty($condition['entity_type'])) {
            $beans['entity_type'] = $condition['entity_type'];
        }
        if (isset($condition['purpose_type']) && !empty($condition['purpose_type'])) {
            $beans['purpose_type'] = $condition['entity_type'];
        }

        if (isset($condition['start_time'])) {
            $beans['create_time'] = ['>=',$condition['start_time']];
        }

        if (isset($condition['end_time'])) {
            $beans['create_time'] = ['<=',$condition['end_time']];
        }

        if (!empty($condition['sk']) && !empty($condition['sk'])) {
            if (common::is_warehouse_id($condition['sk'])) {
                $beans['id'] = $condition['sk'];
            } else {
                $beans['name'] = $condition['sk'];
            }
        }

        return Db::name($this->name)->where($beans)->count('id');
    }

    function is_in($city_id, $coordinate) {
        $condition = [
            'eftype' => EB_TYPE_WAREHOUSE,
            'city_id' => $city_id,
        ];
        $result = himongo::db()->near($coordinate[0], $coordinate[1], 50, 1, $condition);

        if (!empty($result[0])) {
            return common::type_conver($result[0], $this->mongo_field);
        }
        return false;
    }

    var $mongo_field = [
        'id' => 'string',
        'city_id' => 'string',
        'name' => 'string',
        'remark' => 'string',
        'coordinate' => 'array',
        'state' => 'int',
        'purpose_type' => 'string',
        'entity_type' => 'string',
        'purpose' => 'string',
        'capacity' => 'int',
        'quantity' => 'int',
        'address' => 'string',
        'creator_id' => 'string',
        'create_time' => 'string',
        'creator_name' => 'string',
        'updater_id' => 'string',
        'update_time' => 'string',
        'updater_name' => 'string',
        'is_deleted' => 'int',
    ];

}